package com.example.jpademo.repository;

import com.example.jpademo.UserVo;
import com.example.jpademo.entity.User;
import com.example.jpademo.entity.UserDto;
import com.example.jpademo.entity.UserDto2;
import com.example.jpademo.entity.UserDto3;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

public interface UserDao extends JpaRepository<User,Integer>, JpaSpecificationExecutor<User>, Serializable {

    @Query(value = "update User set name = ?1 where id = ?2 ")
    @Modifying(clearAutomatically = true)
    void updateUserName(String name, Integer id);
    @Modifying
    @Transactional
    List<User> removeByName(String name);
    long count();
    UserDto findByName(String name);
    @Query("select new com.example.jpademo.entity.UserDto(u.id, u.name, u.age, u.maxSalary) from User u where u.name = :name and u.age = :age")
    List<UserDto> findByNameAge(String name, Integer age);
    @Query("select u.id, u.name from User u where u.name = :name and u.age = :age")
    List<UserDto2> findByNameAge2(String name, Integer age);
    @Query("select u from User u where u.name = :name and u.age = :age")
    List<UserDto3> findByNameAge3(String name, Integer age);
    @Query("select u.name from User u where u.name in ?1")
    List<String> selectNames(List<String> names);

    int countByName(String name);

    @Query("select u.id as id, u.name as name from User u where u.name = ?1")
    Page<UserDto3> pageQuery(String name, Pageable pageable);

    @Query("select u from User u where u.name = :#{#user.name} and u.age = :#{#user.age}")
    Page<User> fullQuery(User user, Pageable pageable);

    List<UserDto> findByNameIn(List<String> names);

    List<User> findAllByAddressAfterOrderByMaxSalaryDesc(String address);

    List<User> findAllByMaxSalaryGreaterThan(Integer salary);

    @Query(value = "select u.* from t_user u where u.max_salary = ?1", nativeQuery = true)
    User getUserBySalary(Integer salary);

    @Query(value = "select u from User u where u.maxSalary > ?1")
    List<User> maxSalary(Integer salary);

    @Query(value = "select u.name from User u where u.maxSalary > 11")
    List<String> names(Integer salary);

    @Query(value = "select u.* from t_user u where u.max_salary > ?1", nativeQuery = true)
    List<UserVo> listUserVo(Integer salary);
}